package cn.wk.org.service.impl;

import cn.wk.basic.constant.ConstantPethome;
import cn.wk.basic.exception.DiyException;
import cn.wk.basic.service.impl.BaseServiceImpl;
import cn.wk.basic.utils.MD5Utils;
import cn.wk.basic.utils.SendMail;
import cn.wk.basic.utils.StrUtils;
import cn.wk.org.domain.Employee;
import cn.wk.org.domain.Shop;
import cn.wk.org.mapper.EmployeeMapper;
import cn.wk.org.mapper.ShopMapper;
import cn.wk.org.service.IEmployeeService;
import cn.wk.org.service.IShopService;
import cn.wk.user.domain.LoginInfo;
import cn.wk.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    /**
     * 商家入驻
     * @param shop
     */
    @Override
    @Transactional
    public void setTlement(Shop shop) throws DiyException {
        //校验数据（店名重复）
        checkDate(shop);
        //检验该注册商家的手机号是否已经被注册
        checkPhone(shop);
        //先保存店铺信息（店铺中没有员工id）
        super.save(shop);
        //获取员工对象
        Employee admin = shop.getAdmin();
        //然后把该店铺设置给该员工
        admin.setShop(shop);
        //给admin设置盐值
        String complexRandomString = StrUtils.getComplexRandomString(10);
        admin.setSalt(complexRandomString);
        //给admin密码加密
        admin.setPassword(MD5Utils.encrypByMd5(admin.getPassword()+complexRandomString));
        //创建logininf
        LoginInfo loginInfo = creatLoginInfo(admin);
        //保存loginInfo
        loginInfoMapper.save(loginInfo);
        //给admin添加logininfo
        admin.setLogininfo(loginInfo);
        //然后保存员工信息，去拿其主键id
        employeeMapper.save(admin);
        //更新店铺信息（此时店铺中有员工id）
        super.update(shop);
    }

    /**
     * 校验该商家输入的手机号是否被注册
     * @param shop
     */
    private void checkPhone(Shop shop) throws DiyException {
        //先通过shop中的emp电话去数据库查询
        String phone = shop.getAdmin().getPhone();
        List<Employee> emps = employeeMapper.findByPhone(phone);
        if(emps.size() != 0){
            throw new DiyException("该手机号已经被注册");
        }
    }

    /**
     * 用来进行校验数据
     * 判断商家店名是否重名等
     * @param shop
     */
    private void checkDate(Shop shop) throws DiyException {
        Shop shopByName = shopMapper.findByName(shop.getName());
        if(!StringUtils.isEmpty(shopByName)){
            throw new DiyException("店名已经被注册不能重复注册");
        }
    }

    /**
     * 创建logininfo
     * @param admin
     * @return
     */
    private LoginInfo creatLoginInfo(Employee admin) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(admin,loginInfo);
        //设置人员类型
        loginInfo.setType(ConstantPethome.ADMIN);
        return loginInfo;
    }

    @Override
    public List<Shop> toAudit() {
        List<Shop> shops = shopMapper.toAudit();
        return shops;
    }

    @Autowired
    private JavaMailSender javaMailSender;

    @Transactional
    @Override
    public void updateState(Long id) {
        //通过传进来的id去查询该数据，然后把其state状态改成1
        shopMapper.updateState(id);
        //根据id去查询商铺所属员工，然后获取其邮箱
        Shop shopById = shopMapper.findById(id);
        Employee admin = shopById.getAdmin();
        String email = admin.getEmail();
        //给其邮箱发送邮件
//        SendMail.send(email);
        //新建一个普通邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("wjyb3134@qq.com");
        //设置收件人
        mailMessage.setTo(email);
        //设置邮件内容
        mailMessage.setText("恭喜你，你的店铺已经通过审核，请到以下网址进行激活");
        //设置邮件标题
        mailMessage.setSubject("店铺激活成功");
        //发送该邮件
        javaMailSender.send(mailMessage);
    }

    /**
     * 根据logininid查询shop
     * @param id
     * @return
     */
    @Override
    public Shop findByLoginInfoId(Long id) {
        return shopMapper.findByLoginInfoId(id);
    }

}
